![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Bug Proofing Visual Basic: A Guide to Error Handling and Prevention
You can make it easier to verify that the function always returns the correct value by preceding every Exit and End statement with either a value assignment or a comment indicating that the function should return the default value. Then when you inspect the function, you can easily verify that one of these statements is present.
Private Function MyFunction() As Integer
MyFunction = 0 Assume we will return 0.
:
If some_condition Then
MyFunction = 1 Return 1.
Exit Function
End If
:
If some_other_condition Then
Return the default value.
Exit Function
End If
:
Return the default value.
End Function
Some functions modify their return values in complicated ways before returning. Try to restructure the code to match one of the previous simple formats. If you cannot do that, provide extensive comments that help the reader keep track of the functions return value as it changes. Beware Foreign SyntaxSome statements are valid in Visual Basic and in another programming language, but they have different meanings in the two languages. For example, consider the following code:
Dim A As Integer
Dim B As Integer
A = B = 1
The statement A = B = 1 is valid in Visual Basic, C, and C++. In C and C++, this statement assigns the value of 1 to the variables B and A. In Visual Basic, however, this statement is equivalent to A = (B = 1). The variable A is assigned the value of the Boolean expression B = 1. Since B is automatically initialized to 0 when it is created, B does not equal 1 so the expression is False. That means A is assigned the value False and B remains unchanged. In the end, both variables have the value 0. If you are familiar with another language, beware of that other languages syntax creeping into your Visual Basic code. Visual Basic may execute the code, but you may not get the results you expect. Never Use GoTo, Except...GoTo statements have a deservedly bad reputation. Improperly used GoTo statements can make a routine totally incomprehensible. They confuse the flow of control through the routine and turn simple loops into confusing tangles. The following code shows two implementations of the selectionsort algorithm. The first uses GoTo statements instead of the For loops used by the second.
Public Sub SelectionSort(ByRef numbers() As Integer)
Dim i As Integer
Dim j As Integer
Dim smallest_value As Integer
Dim smallest_index As Integer
i = LBound(numbers)
ILoop:
smallest_value = numbers(i)
smallest_index = i
j = i
GoTo SaveSmallest
JLoop:
If numbers(j) >= smallest_value Then GoTo SkipIt
SaveSmallest:
smallest_value = numbers(j)
smallest_index = j
SkipIt:
j = j + 1
If j <= UBound(numbers) Then GoTo JLoop
numbers(smallest_index) = numbers(i)
numbers(i) = smallest_value
i = i + 1
If i <= UBound(numbers) Then GoTo ILoop
End Sub
Public Sub SelectionSort(ByRef numbers() As Integer)
Dim i As Integer
Dim j As Integer
Dim smallest_value As Integer
Dim smallest_index As Integer
For i = LBound(numbers) To UBound(numbers)
smallest_value = numbers(i)
smallest_index = i
For j = i + 1 To UBound(numbers)
If numbers(j) < smallest_value Then
smallest_value = numbers(j)
smallest_index = j
End If
Next j
numbers(smallest_index) = numbers(i)
numbers(i) = smallest_value
Next i
End Sub
Both of these subroutines work, but the GoTo statements in the first make it much harder to read. They also make it take almost twice as long to run. Do not use GoTos to control a routines flow. Use For and While loops instead. There are only a few reasons you should ever use a GoTo statement in Visual Basic and some of them are discussed in the following sections. Some ways in which you should never use a GoTo include:
Use GoTo for Error HandlingVisual Basic uses On Error GoTo statements to specify error handlers. When an error occurs, the routine essentially performs a GoTo to jump into the error-handling code. This special use of GoTo is consistent and easy to understand. Programmers who later read a routine that uses On Error GoTo will know that the GoTo leads to an error handler. You can use this form of GoTo because it will not cause confusion. A routine also executes an implied GoTo when an error handler ends. If the program uses a Resume or Resume Next statement, execution resumes near where the error occurred. These are standard ways for leaving an error handler, so they will not cause confusion either. The Resume statement also lets a program resume at a specific line. For example, the statement Resume StartHere makes the program jump to the line labeled StartHere. This transfer of control is not standard and is more confusing than simple Resume or Resume Next statements, so you should use it only when absolutely necessary. One situation in which it may make sense to resume at a specific line is when the routine must perform some cleanup before it exits. For instance, the following code opens a file and reads data from it. If the routine encounters an error, it resumes execution at the beginning of the cleanup code. That ensures that the file is properly closed before the routine ends. Note that the first thing the cleanup code does is disable error handling with the On Error Resume Next statement. If it did not and the routine encountered an error while inside the cleanup code, it would jump to the error handler. After presenting its message, the error handler would resume execution at the cleanup code again. The program would be stuck in an infinite loop.
Private Sub LoadData(ByVal file_name As String)
Dim fnum As Integer
Open a file.
fnum = FreeFile
Open file_name For Input As #fnum
Read from the file, process data, etc.
On Error GoTo ReadError
:
Continue into the clean up code.
CloseFile:
Close the file.
On Error Resume Next
Close #fnum
Exit Sub
ReadError:
MsgBox Error & Str$(Err.Number) & _
vbCrLf & Err.Description
Go to the file cleanup code.
Resume CloseFile
End Sub
|
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.
|